In [ ]:
from PIL import Image, ImageEnhance
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
import pandas as pd
import math
import requests
import json
import re
import csv
directory_path = os.getcwd()
parent_directory_path = os.path.dirname(directory_path)
csv_path = os.path.join(parent_directory_path, 'Model\\condo_data_new_FINAL_test.csv')
gt_masked_image_path = os.path.join(parent_directory_path, 'Model\\rd\\test')
generated_image_path = os.path.join(parent_directory_path, 'Model\\rd\\final_road_output_3')
# Read the CSV file
data = pd.read_csv(csv_path)
# Function to extract the numeric part of the filename
def extract_numeric_part(filename):
numeric_part = ''.join(filter(str.isdigit, filename))
return int(numeric_part) if numeric_part else None
def create_binary_mask(arr, target_color, threshold=30):
lower_bound = np.array(target_color) - threshold
upper_bound = np.array(target_color) + threshold
mask = (arr[:, :, :3] >= lower_bound) & (arr[:, :, :3] <= upper_bound)
return np.all(mask, axis=-1)
def extract_building_regions(arr, target_color, threshold=10):
lower_bound = np.array(target_color) - threshold
upper_bound = np.array(target_color) + threshold
mask = (arr[:, :, :3] >= lower_bound) & (arr[:, :, :3] <= upper_bound)
return np.all(mask, axis=-1)
# def find_max_building_storeys(gpr):
# max_building_storeys= 0
# if gpr >= 0 and gpr < 1.4:
# max_building_storeys = 5
# elif gpr >= 1.4 and gpr < 1.6:
# max_building_storeys = 12
# elif gpr >= 1.6 and gpr < 2.1:
# max_building_storeys = 24
# elif gpr >= 2.1 and gpr < 2.8:
# max_building_storeys = 36
# elif gpr >= 2.8:
# max_building_storeys = 48 ## by right got no limit
# return max_building_storeys
def masked_rgb(simp_gpr):
rgb = [0,0,0]
if simp_gpr == 1.4:
rgb = [0,255,0]
elif simp_gpr == 1.6:
rgb = [200,130,60]
elif simp_gpr == 2.1:
rgb = [0,0,0]
elif simp_gpr == 2.8:
rgb = [255,0,0]
elif simp_gpr == 3.0:
rgb =[0,0,255]
return rgb
'''
pink, [255, 10, 169]
brown, [200,130,60]
cyan, [0,255,255]
red, [255,0,0]
black, [0,0,0]
green, [0,255,0]
blue, [0,0,255]
yellow, [255, 255, 0]
'''
# absolute_accuracies = []
# losses =[]
# images =[]
# sanity_ratios =[]
gprs =[]
generated_gprs =[]
sanity_ratios =[]
# Iterate through the images in the generated_image_path
for image_file in os.listdir(generated_image_path):
if image_file.endswith('.png'):
image_index = extract_numeric_part(image_file)
# Construct the path for the corresponding masked image
gt_mask_image_filename = f"{image_index}.png"
gt_mask_image = os.path.join(gt_masked_image_path, gt_mask_image_filename)
open_gt_mask_image = Image.open(gt_mask_image)
mask_crop_box = (512, 0, 1024, 512) # right side
mask_image = open_gt_mask_image.crop(mask_crop_box) #gt_mask is concatenated gt and mask
gt_crop_box = (0, 0, 512, 512) # left side
gt_image = open_gt_mask_image.crop(gt_crop_box)
generated_image = os.path.join(generated_image_path, image_file)
generated_image = Image.open(generated_image)
# Check if the image index matches any index in the CSV
matched_row = data[data['key1'] == image_index]
if not matched_row.empty:
# Extract the GPR value for the matched row
gpr_value = matched_row['GPR'].iloc[0]
storey = matched_row['storeys'].iloc[0]
simplified_gpr_value = matched_row['simp_gpr'].iloc[0]
actual_site_area = matched_row['area'].iloc[0]
actual_site_area = actual_site_area.replace(',', '')
actual_site_area = float(actual_site_area[:-4])
gpr_value = float(gpr_value)
storey = int(storey)
mask_array = np.array(mask_image)
generated_array = np.array(generated_image)
mask_color = masked_rgb(simplified_gpr_value)
site_mask = create_binary_mask(mask_array, mask_color)
site_area_array = generated_array.copy()
site_area_array[~site_mask] = [255, 255, 255, 255] # making non-masked region white RMB ITS 4 CHANNELS NOW
site_area_image = Image.fromarray(site_area_array)
mask_color = [255, 10, 169] # pink
building_mask = extract_building_regions(site_area_array, mask_color)
buildings_image = Image.fromarray(building_mask)
plt.figure(figsize=(20, 5))
plt.subplot(1, 4, 1)
plt.imshow(mask_image)
plt.title('Mask Image')
plt.axis('off')
plt.subplot(1, 4, 2)
plt.imshow(gt_image)
plt.title('GT Image')
plt.axis('off')
plt.subplot(1, 4, 3)
plt.imshow(generated_image)
plt.title('Generated Image')
plt.axis('off')
plt.subplot(1, 4, 4)
plt.imshow(buildings_image, cmap='gray')
plt.title('Buildings Image')
plt.axis('off')
plt.show()
# accuracy
building_pixels = np.sum(building_mask)
mask_pixels = np.sum(site_mask)
msq_per_pixel = actual_site_area/mask_pixels
building_area = msq_per_pixel * building_pixels
#max_storeys = find_max_building_storeys(gpr_value)
generated_gpr = building_area*storey/actual_site_area
gprs.append(gpr_value)
generated_gprs.append(generated_gpr)
# if generated_gpr == 0:
# accuracy = 0
# else:
# accuracy = (gpr_value - generated_gpr) / gpr_value #gpr_value is the target gpr
# loss =
# images.append(image_file)
# absolute_accuracy = abs(accuracy)
# absolute_accuracies.append(absolute_accuracy)
print(f'Image: {image_file}, GPR: {gpr_value}, Simplified GPR: {simplified_gpr_value}, Storeys:{storey}, Site area: {actual_site_area}, Building pixels: {building_pixels}, Mask pixels: {mask_pixels}, Generated GPR: {generated_gpr}')
#sanity check. ratios should be about 0.75
ratio = mask_pixels/actual_site_area
sanity_ratios.append(ratio)
total_data = len(gprs)
accuracies = []
absolute_error =[]
square_error =[]
for tar_gpr, gen_gpr in zip(gprs, generated_gprs):
accuracies.append(abs((tar_gpr-gen_gpr)/tar_gpr))
absolute_error.append(abs(tar_gpr-gen_gpr))
square_error.append((tar_gpr-gen_gpr)**2)
accuracy = sum(accuracies)/total_data
mean_abs_error = sum(absolute_error)/total_data
root_squared_error = math.sqrt(sum(square_error)/total_data)
print(f"Accuracies:{accuracies} \nSquare error:{square_error} \nAbsolute error:{absolute_error} ")
print(f"\nAccuracy:{accuracy} MAE:{mean_abs_error} RMSE:{root_squared_error}")
Image: 1040.png, GPR: 1.4, Simplified GPR: 1.4, Storeys:5, Site area: 23065.1, Building pixels: 3577, Mask pixels: 16203, Generated GPR: 1.103807936801827
Image: 1074.png, GPR: 2.5, Simplified GPR: 2.8, Storeys:12, Site area: 37265.0, Building pixels: 4678, Mask pixels: 27286, Generated GPR: 2.0573187715311883
Image: 1076.png, GPR: 2.8, Simplified GPR: 2.8, Storeys:36, Site area: 10414.2, Building pixels: 1242, Mask pixels: 8468, Generated GPR: 5.280113367973548
Image: 1102.png, GPR: 1.6, Simplified GPR: 1.6, Storeys:12, Site area: 6157.3, Building pixels: 1611, Mask pixels: 4749, Generated GPR: 4.070751737207833
Image: 1180.png, GPR: 3.0, Simplified GPR: 3.0, Storeys:15, Site area: 19547.0, Building pixels: 4229, Mask pixels: 14230, Generated GPR: 4.457835558678847
Image: 1379.png, GPR: 1.4, Simplified GPR: 1.4, Storeys:5, Site area: 17455.9, Building pixels: 3348, Mask pixels: 12216, Generated GPR: 1.3703339882121808
Image: 145.png, GPR: 2.8, Simplified GPR: 2.8, Storeys:15, Site area: 22094.4, Building pixels: 3876, Mask pixels: 16171, Generated GPR: 3.5953249644425207
Image: 1484.png, GPR: 3.0, Simplified GPR: 3.0, Storeys:17, Site area: 10097.1, Building pixels: 2596, Mask pixels: 7571, Generated GPR: 5.829084665169727
Image: 1602.png, GPR: 3.0, Simplified GPR: 3.0, Storeys:17, Site area: 13564.8, Building pixels: 3644, Mask pixels: 9875, Generated GPR: 6.273215189873417
Image: 1655.png, GPR: 2.1, Simplified GPR: 2.1, Storeys:18, Site area: 27418.2, Building pixels: 6357, Mask pixels: 21711, Generated GPR: 5.2704159181981485
Image: 1670.png, GPR: 2.8, Simplified GPR: 2.8, Storeys:13, Site area: 17940.2, Building pixels: 2726, Mask pixels: 11713, Generated GPR: 3.025527192008879
Image: 1796.png, GPR: 2.8, Simplified GPR: 2.8, Storeys:17, Site area: 13877.2, Building pixels: 1853, Mask pixels: 9260, Generated GPR: 3.40183585313175
Image: 1811.png, GPR: 1.4, Simplified GPR: 1.4, Storeys:5, Site area: 7255.7, Building pixels: 784, Mask pixels: 5237, Generated GPR: 0.7485201451212528
Image: 1876.png, GPR: 2.1, Simplified GPR: 2.1, Storeys:19, Site area: 10502.8, Building pixels: 2126, Mask pixels: 8220, Generated GPR: 4.914111922141119
Image: 191.png, GPR: 3.5, Simplified GPR: 3.0, Storeys:18, Site area: 13000.3, Building pixels: 3074, Mask pixels: 9133, Generated GPR: 6.058469287200262
Image: 2000.png, GPR: 3.0, Simplified GPR: 3.0, Storeys:17, Site area: 13241.8, Building pixels: 3548, Mask pixels: 9581, Generated GPR: 6.295376265525519
Image: 434.png, GPR: 2.1, Simplified GPR: 2.1, Storeys:16, Site area: 39401.6, Building pixels: 7586, Mask pixels: 28557, Generated GPR: 4.250306404734391
Image: 489.png, GPR: 2.1, Simplified GPR: 2.1, Storeys:15, Site area: 28692.65, Building pixels: 4479, Mask pixels: 20400, Generated GPR: 3.2933823529411765
Image: 491.png, GPR: 3.0, Simplified GPR: 3.0, Storeys:16, Site area: 18747.8, Building pixels: 3588, Mask pixels: 12952, Generated GPR: 4.432365657813465
Image: 568.png, GPR: 3.4, Simplified GPR: 3.0, Storeys:19, Site area: 14344.0, Building pixels: 3511, Mask pixels: 10424, Generated GPR: 6.399558710667691 Accuracies:[0.21156575942726638, 0.1770724913875247, 0.8857547742762671, 1.5442198357548955, 0.48594518622628247, 0.02119000841987081, 0.2840446301580432, 0.9430282217232424, 1.0910717299578057, 1.509721865808642, 0.0805454257174568, 0.21494137611848216, 0.4653427534848194, 1.3400532962576757, 0.7309912249143606, 1.098458755175173, 1.0239554308259005, 0.5682773109243697, 0.477455219271155, 0.8822231501963796] Square error:[0.08772973830159046, 0.1959666700386563, 6.150962318001095, 6.104614146915524, 2.1252845161484673, 0.0008800722553950239, 0.6325417990654971, 8.003720042698507, 10.71393767921807, 10.051537094364209, 0.0508625143354098, 0.3622063941148214, 0.4244260013128335, 7.919225910336784, 6.545765093547018, 10.859504731388915, 4.6238176342417425, 1.4241614403114187, 2.0516713776834, 8.99735245874242] Absolute error:[0.29619206319817293, 0.44268122846881175, 2.480113367973548, 2.470751737207833, 1.4578355586788474, 0.029666011787819135, 0.7953249644425209, 2.8290846651697272, 3.273215189873417, 3.1704159181981484, 0.22552719200887905, 0.60183585313175, 0.6514798548787472, 2.814111922141119, 2.5584692872002623, 3.295376265525519, 2.150306404734391, 1.1933823529411764, 1.432365657813465, 2.999558710667691] Accuracy:0.7017929223012805 MAE:1.7583847103020922 RMSE:2.0895713392107695